home *** CD-ROM | disk | FTP | other *** search
- Path: news.ios.com!usenet
- From: John Leonard <leonardj@tribeca.ios.com>
- Newsgroups: comp.lang.c++
- Subject: aborting a constructor
- Date: Mon, 29 Jan 1996 23:50:51 -0500
- Organization: 12th of Nov, Inc
- Message-ID: <310DA3AB.6E32@tribeca.ios.com>
- NNTP-Posting-Host: ppp-57.ts-7.hck.idt.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b4 (Win95; I)
-
- In an earlier post, I asked how you use 'new' and a constructor in order to deal with the
- following situation:
- You have a class, let's call it WindowClass, which, when it is instantiated, does the
- following:
- 1: It opens the file "filename", and gets its size.
- 2: It allocates memory to hold "filename".
- 3: It loads "filename" into that memory.
- 4: It opens a window to display "filename".
- Now let's suppose that after it performs steps 1 thru 3 it cannot open the window, what does it
- do? Furthermore, how do you code for this kind of situation in C++ in general? My first thought was
- that the constructor should de-allocate all of the resources that it had received so far. Then it
- should throw an exception. The question that I had was: what's going to happen to the memory that
- was allocated for the object's data members? I nother words: what happens to an object if its
- constructor aborts? Is it automatically deleted? I still haven't really gotten to the bottom of that
- question but I want to pose a possible solution to this dilemma and see if it works.
- Instead of the course of action described above, let's say that you have the constructor
- execute the following statements:
-
- delete this;
- throw(error);
-
- and you have the destructor of the class de-allocate the objects. I'd like to know if this would
- work.
- John
-